//
//  NewFeatureController.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/7.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    
    var control : UIPageControl!
    
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0.0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView?.backgroundColor = UIColor.greenColor()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView?.pagingEnabled = true
        self.collectionView?.bounces = false
        self.collectionView?.showsHorizontalScrollIndicator = false
        
        setUpPageControl()
    }

    func setUpPageControl(){
        let control = UIPageControl()

        control.numberOfPages = 4
        control.pageIndicatorTintColor = UIColor.blackColor()
        control.currentPageIndicatorTintColor = UIColor.redColor()
        
        control.center = CGPointMake(self.view.width * 0.5, self.view.height - 10)
        self.control = control
        self.view.addSubview(control)

    }
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5)
        
        // 设置页数
        control.currentPage = page
    }
    
    
    
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        //let screenH = UIScreen.mainScreen().bounds.size.height
        let imageName = "new_feature_\(indexPath.row + 1)"
        
        cell.image = UIImage(named: imageName)
        cell.setIndexPath(indexPath, count: 4)
        return cell
    }




}
